import { router } from '../router';
import store from '../store'


function arryRemove(arry, val) {
    var index = arry.indexOf(val);
    if (index > -1) {
        arry.splice(index, 1);
    }
};

export function anyPermission(values) {
    if (values.length === 0) {
        return true
    }
    let permsisions = store.state.user.permsisions
    for (let index = 0; index < values.length; index++) {
        const element = values[index];
        if (permsisions.indexOf(element) > -1) {
            return true
        }
    }
    return false
}

export function allPermission(values) {
    if (values.length === 0) {
        return true
    }
    let permsisions = store.state.user.permsisions
    let deep_values = []
    Object.assign(deep_values, values)

    for (let index = 0; index < values.length; index++) {
        const element = values[index];
        if (permsisions.indexOf(element) >= 0) {
            arryRemove(deep_values, element)
        }
    }
    if (deep_values.length !== 0) {
        return false
    }
    return true
}
// 缓存白名单
export function getCacheWhiteList(routes) {
    let cacheWhiteList = []
    for (const key in routes) {
        const element = routes[key];
        let cache = true
        let have_children = element.children.length > 0
        let childrenCacheWhiteList = []
        if (have_children) {
            childrenCacheWhiteList = getCacheWhiteList(element.children)
        }
        if (!have_children) {
            if (cache) {
                let name = element.component.name
                if (element.component.name) {
                    cacheWhiteList.push(name)
                }
            }
        }
        cacheWhiteList = cacheWhiteList.concat(childrenCacheWhiteList)
    }
    return cacheWhiteList
}

// 路由及菜单权限处理
export function nestedRouteHandle(nestedRoutes, base_url = null) {
    let routes = []
    let sidebars = []
    for (const key in nestedRoutes) {
        let children_res = { routes: [], sidebars: [] }
        const element = nestedRoutes[key];
        let path = element.path
        if (base_url !== null) {
            path = [base_url, element.path].join('/')
        }
        if (element.children) {
            children_res = nestedRouteHandle(element.children, path)
        }
        let isMenu = true
        let permissions = []
        let isFK = false
        let title = '未命名模块'
        let icon = 'el-icon-bangzhu'
        let cache = false
        let activeMenu = path
        let sortWeight = 0
        if (element.meta) {
            // 替换默认值
            isMenu = element.meta.isMenu === false ? false : true
            permissions = element.meta.permissions ? element.meta.permissions : []
            isFK = element.meta.path ? true : false
            path = element.meta.path ? element.meta.path : path
            activeMenu = element.meta.activeMenu ? element.meta.activeMenu : activeMenu
            title = element.meta.title ? element.meta.title : title
            icon = element.meta.icon ? element.meta.icon : icon
            cache = element.meta.cache === true ? element.meta.cache : cache
            sortWeight = element.meta.sortWeight ? element.meta.sortWeight : sortWeight
        }
        // 检查是否有权限 如果没有权限就跳过
        if (!anyPermission(permissions)) {
            continue
        }
        // 不为菜单就只注册路由
        if (isMenu) {
            sidebars.push({
                isFK: isFK,
                path: path,
                title: title,
                icon: icon,
                activeMenu: activeMenu,
                sortWeight: sortWeight,
                children: children_res.sidebars
            })
        } else {
            // 如果此不为菜单则子级自动提升
            if (children_res.sidebars) {
                for (let i in children_res.sidebars) {
                    let children_sidebar = children_res.sidebars[i]
                    sidebars.push(children_sidebar)
                }
            }
        }

        element.children = children_res.routes
        routes.push(element)
    }
    sidebars.sort((a, b) => {
        return b.sortWeight - a.sortWeight
    })
    return { routes, sidebars }
}
